<template>
  <div class="table-properties">
    <el-form label-position="top">
      <el-form-item label="表格列">
        <div v-for="(column, index) in properties.columns" :key="index" class="column-item">
          <el-row :gutter="10">
            <el-col :span="8">
              <el-input
                v-model="column.label"
                placeholder="列标题"
                @change="updateProperties" />
            </el-col>
            <el-col :span="8">
              <el-input
                v-model="column.prop"
                placeholder="字段名"
                @change="updateProperties" />
            </el-col>
            <el-col :span="5">
              <el-input-number
                v-model="column.width"
                :min="50"
                placeholder="宽度"
                @change="updateProperties" />
            </el-col>
            <el-col :span="3">
              <el-button type="danger" @click="removeColumn(index)">
                <el-icon><Delete /></el-icon>
              </el-button>
            </el-col>
          </el-row>
        </div>
        
        <el-button type="primary" @click="addColumn">
          <el-icon><Plus /></el-icon>
          添加列
        </el-button>
      </el-form-item>

      <el-form-item label="样式设置">
        <el-checkbox
          v-model="properties.border"
          @change="updateProperties">
          显示边框
        </el-checkbox>
        
        <el-checkbox
          v-model="properties.stripe"
          @change="updateProperties">
          斑马纹
        </el-checkbox>
      </el-form-item>

      <el-form-item label="分页设置">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-input-number
              v-model="properties.pageSize"
              :min="1"
              placeholder="每页条数"
              @change="updateProperties" />
          </el-col>
          <el-col :span="12">
            <el-select
              v-model="properties.pageSizes"
              multiple
              placeholder="可选每页条数"
              @change="updateProperties">
              <el-option :value="10" label="10条/页" />
              <el-option :value="20" label="20条/页" />
              <el-option :value="50" label="50条/页" />
              <el-option :value="100" label="100条/页" />
            </el-select>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { Delete, Plus } from '@element-plus/icons-vue'

const props = defineProps({
  value: {
    type: Object,
    default: () => ({
      columns: [],
      border: true,
      stripe: true,
      pageSize: 10,
      pageSizes: [10, 20, 50, 100]
    })
  }
})

const emit = defineEmits(['update'])

const properties = ref({ ...props.value })

watch(() => props.value, (newVal) => {
  properties.value = { ...newVal }
}, { deep: true })

const addColumn = () => {
  properties.value.columns.push({
    label: '',
    prop: '',
    width: 100
  })
  updateProperties()
}

const removeColumn = (index) => {
  properties.value.columns.splice(index, 1)
  updateProperties()
}

const updateProperties = () => {
  emit('update', { ...properties.value })
}
</script>

<style scoped>
.column-item {
  margin-bottom: 10px;
}
</style> 